<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./swiper/css/swiper.css">
  <style>
    .home-swiper{
      width: 600px;
      height: 300px;
      margin: 20px auto;
      overflow: hidden;
    }
    .swiper-slide img{
      width: 600px;
      height: 300px;
    }
   
  </style>
</head>
<body>
  <div id="app">
  
    <home-page></home-page>
  </div>
  <script src="./vue.global.js"></script>
  <script src="./axios.js"></script>
  <script src="./swiper/js/swiper.js"></script>
  <script>
    // https://api.it120.cc/conner/banner/list
    const { createApp } = Vue;
    const app = createApp({
    })
    const HomePage = {
      template: `
        <div>
          <h2>首页</h2>  
          <div class="swiper home-swiper">
            <div class="swiper-wrapper">
              <div 
                class="swiper-slide"
                v-for="banner in banners"
                :key="banner.id"
              >
                  <img :src="banner.picUrl"/>
              </div>
            </div>
          </div>
        </div>
      `,
      data(){
        return {
          swiper: null,
          banners: []
        }
      },
      methods: {
        initSwiper(){
          this.swiper = new Swiper('.home-swiper')
        },
        fetchBanners(){
          // 请求轮播数据
          axios.get('https://api.it120.cc/conner/banner/list').then(res => {
            if (res.data.code ===0){
              this.banners = res.data.data
              // 更新swiper
              /* this.$nextTick(() => {
                this.swiper.update();
              }) */
            }
          })
        }
      },
      watch: {
        banners: {
          handler(){
            this.$nextTick(() => {
                this.swiper.update();
              })
          },
          deep: true
        }
      },
      created(){
        this.fetchBanners();
      },
      mounted(){
        this.initSwiper();
      }
    }
    app.component('HomePage', HomePage)
    app.mount("#app")
  </script>
</body>
</html>